﻿using GodSharp;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZK.Common;
using ZK.InterfaceServer;

namespace ZK.HardWareHelp
{
    /// <summary>
    /// 山东领军 红绿交通灯
    /// </summary>
    public class TrafficLight
    {
        private readonly GodSerialPort _serialPort;
        private bool _isOpened;
        public int lightStatus = 0;

        public TrafficLight(Common.HaredWareCommonM trafficLightInfo)
        {
            try
            {
                _serialPort =
                    new GodSerialPort(trafficLightInfo.PortName, Convert.ToInt32(trafficLightInfo.BaudRate), Parity.None,
                    Convert.ToInt32(trafficLightInfo.DataBits), StopBits.One)
                    {
                        DataFormat = SerialPortDataFormat.Hex,
                        RtsEnable = false,
                        DtrEnable = true
                    };
                Open();
            }
            catch (Exception ex)
            {
                CSApiServer.AddHardWareLog(trafficLightInfo.Name, 1, ex.Message);
                Common.Log4.LogManage.WriteErr("红绿灯初始化 " + ex.ToString() + " 红绿灯信息 串口号：" + trafficLightInfo.PortName);
                //  MessageDialog.MessageBoxShow("红绿灯初始化失败！");
                Common.DelegateHelper.InfoMessageEvent?.Invoke("红绿灯初始化失败！");
            }
        }


        public void RedLight()
        {
            try
            {
                lightStatus = 1;
                //_serialPort.Write(new byte[]
                //{
                //0x77,
                //0x75,
                //0x30,
                //0x00,
                //0x04,
                //0x0C,
                //0x0C,
                //0x0C,
                //0x0D,
                //0x0A
                //});
                _serialPort.Write(new byte[]
                {
                0x77,
                0x75,
                0x30,
                0x00,
                0x0C,
                0x04,
                0x0C,
                0x0C,
                0x0D,
                0x0A
                });
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("红绿灯红灯亮 " + ex.ToString());
                Common.DelegateHelper.InfoMessageEvent?.Invoke("红绿灯红灯亮出错");
            }
        }

        public void GreenLight()
        {
            try
            {
                lightStatus = 2;
                //_serialPort.Write(new byte[]
                //{
                //0x77,
                //0x75,
                //0x30,
                //0x00,
                //0x0C,
                //0x0C,
                //0x0C,
                //0x0C,
                //0x0D,
                //0x0A
                //});

                _serialPort.Write(new byte[]
                {
                0x77,
                0x75,
                0x30,
                0x00,
                0x04,
                0x0C,
                0x0C,
                0x0C,
                0x0D,
                0x0A
                });
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("红绿灯绿灯亮 " + ex.ToString());
                Common.DelegateHelper.InfoMessageEvent?.Invoke("红绿灯绿灯亮出错");
            }
        }

        public void LightOff()
        {
            try
            {
                _serialPort.Write(new byte[]
                {
                0x77,
                0x75,
                0x30,
                0x00,
                0x00,
                0x00,
                0x00,
                0x00,
                0x0D,
                0x0A
                });
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("红绿灯灭灯" + ex.ToString());
                Common.DelegateHelper.InfoMessageEvent?.Invoke("红绿灯灭灯出错");
            }
        }

        public void Open()
        {
            _isOpened = _serialPort.Open();
            ReopenIfClosed();
        }

        public void Close()
        {
            _isOpened = !_serialPort.Close();
        }

        private void ReopenIfClosed()
        {
            if (_isOpened)
            {
                //await Task.Delay(1000);
                //await Task.Run(() =>
                //{
                //    if (!_serialPort.IsOpen)
                //    {
                //        _serialPort.Close();
                //        _serialPort.Open();
                //    }
                //    ReopenIfClosed();
                //});
            }
            else
            {
                _serialPort.Close();
            }
        }
    }
}
